package com.lyb.ui.controller;

import com.lyb.constant.SystemConstant;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;
import java.util.ResourceBundle;

/**
 * 主页面的控制器
 *
 * @author Dayun Mr.Alpaca
 * @version 1.4.2
 */
public class MainController implements Initializable {
    @FXML
    private Label time;
    @FXML
    public MenuItem alpacaItem;
    @FXML
    public MenuItem dayunItem;
    @FXML
    public MenuItem ahuangItem;
    @FXML
    public MenuItem potatoItem;
    @FXML
    public MenuItem zhengItem;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        Timeline timeline = new Timeline();
        KeyFrame keyFrame = new KeyFrame(Duration.millis(500), event -> {
            String curTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            time.setText(curTime);
        });
        timeline.getKeyFrames().add(keyFrame);
        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.setAutoReverse(true);
        timeline.play();
    }

    /**
     * 磁盘功能页面
     *
     * @param event 事件
     * @throws IOException (忽略)
     */
    @FXML
    public void openDisk(MouseEvent event) throws IOException {
        if (event.getClickCount() == 2 && "PRIMARY".equals(event.getButton().name())) {
            Stage stage = new Stage();
            Parent root = FXMLLoader.load(getClass().getResource("/view/disk.fxml"));
            stage.setTitle("磁盘界面");
            Scene scene = new Scene(root);
            scene.getStylesheets().add("/css/UI.css");
            stage.setResizable(false);
            stage.setScene(scene);
            stage.show();
        }
    }

    /**
     * 内存功能页面
     *
     * @param event 事件
     * @throws IOException (忽略)
     */
    @FXML
    public void openMemory(MouseEvent event) throws IOException {
        if (event.getClickCount() == 2 && "PRIMARY".equals(event.getButton().name())) {
            Stage stage = new Stage();
            Parent root = FXMLLoader.load(getClass().getResource("/view/memory.fxml"));
            stage.setTitle("内存界面");
            Scene scene = new Scene(root);
            scene.getStylesheets().add("/css/UI.css");
            stage.setResizable(false);
            stage.setScene(scene);
            stage.show();
        }
    }

    /**
     * CPU功能页面
     *
     * @param event 事件
     * @throws IOException (忽略)
     */
    @FXML
    public void openCpu(MouseEvent event) throws IOException {
        if (event.getClickCount() == 2 && "PRIMARY".equals(event.getButton().name())) {
            Stage stage = new Stage();
            Parent root = FXMLLoader.load(getClass().getResource("/view/cpu.fxml"));
            stage.setTitle("CPU界面");
            Scene scene = new Scene(root);
            scene.getStylesheets().add("/css/UI.css");
            stage.setResizable(false);
            stage.setScene(scene);
            stage.show();
        }
    }

    /**
     * 命令行功能页面
     *
     * @param event 事件
     * @throws IOException (忽略)
     */
    @FXML
    public void openCmd(MouseEvent event) throws IOException {
        if (event.getClickCount() == 2 && "PRIMARY".equals(event.getButton().name())) {
            Stage stage = new Stage();
            Parent root = FXMLLoader.load(getClass().getResource("/view/cmd.fxml"));
            stage.setTitle("命令行界面");
            Scene scene = new Scene(root);
            scene.getStylesheets().add("/css/UI.css");
            stage.setResizable(false);
            stage.setScene(scene);
            stage.show();
        }
    }

    /**
     * 设备功能页面
     *
     * @param event 事件
     * @throws IOException (忽略)
     */
    @FXML
    public void openDevice(MouseEvent event) throws IOException {
        if (event.getClickCount() == 2 && "PRIMARY".equals(event.getButton().name())) {
            Stage stage = new Stage();
            Parent root = FXMLLoader.load(getClass().getResource("/view/device.fxml"));
            stage.setTitle("设备界面");
            Scene scene = new Scene(root);
            scene.getStylesheets().add("/css/UI.css");
            stage.setResizable(false);
            stage.setScene(scene);
            stage.show();
        }
    }

    /**
     * 日志功能页面
     *
     * @param event 事件
     * @throws IOException (忽略)
     */
    @FXML
    public void openLog(MouseEvent event) throws IOException {
        if (event.getClickCount() == 2 && "PRIMARY".equals(event.getButton().name())) {
            Stage stage = new Stage();
            Parent root = FXMLLoader.load(getClass().getResource("/view/log.fxml"));
            stage.setTitle("日志界面");
            Scene scene = new Scene(root);
            scene.getStylesheets().add("/css/UI.css");
            stage.setResizable(false);
            stage.setScene(scene);
            stage.show();
        }
    }

    /**
     * 关机事件
     * 执行系统关机指令
     *
     * @param event 事件
     */
    @FXML
    public void shutDown(MouseEvent event) {
        /* 提示弹窗 */
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "", new ButtonType("取消", ButtonBar.ButtonData.NO),
                new ButtonType("确定", ButtonBar.ButtonData.YES));
        /* 设置警告窗口的标题 */
        alert.setTitle("提示");
        /* 设置提示信息 */
        alert.setHeaderText("是否关机？");
        /* 必须点开才能继续操作 */
        Optional<ButtonType> result = alert.showAndWait();
        /* 关闭所有窗口 */
        if (result.get().getButtonData().equals(ButtonBar.ButtonData.YES)) {
            SystemConstant.ALPACA_COMMANDS.get(4).go(new String[]{"shutdown"});
        }
    }

    /**
     * Doc事件
     * 跳转到指定网页
     *
     * @param event 事件
     */
    @FXML
    public void openDoc(ActionEvent event) {
        Desktop desktop = Desktop.getDesktop();
        if (Desktop.isDesktopSupported()) {
            try {
                desktop.browse(new URI("https://apidoc.gitee.com/birds-of-prey/simulation-os/"));
            } catch (IOException | URISyntaxException ignore) {
            }
        }
    }

    /**
     * Help事件
     * 跳转到指定网页
     *
     * @param event 事件
     */
    @FXML
    public void openHelp(ActionEvent event) {
        Desktop desktop = Desktop.getDesktop();
        if (Desktop.isDesktopSupported()) {
            try {
                desktop.browse(new URI("https://gitee.com/birds-of-prey/simulation-os"));
            } catch (IOException | URISyntaxException ignore) {
            }
        }
    }

    /**
     * Alpaca事件
     * 弹出图片
     *
     * @param actionEvent 事件
     */
    @FXML
    public void openAlpaca(ActionEvent actionEvent) {
        VBox vBox = new VBox();
        vBox.getChildren().addAll(new ImageView("/pics/Alpaca.jpg"));
        Stage stage = new Stage();
        stage.setScene(new Scene(vBox));
        stage.setTitle("Alpaca");
        stage.setResizable(false);
        stage.showAndWait();
    }

    /**
     * Dayun事件
     * 弹出图片
     *
     * @param actionEvent 事件
     */
    @FXML
    public void openDayun(ActionEvent actionEvent) {
        VBox vBox = new VBox();
        vBox.getChildren().addAll(new ImageView("/pics/Dayun.jpg"));
        Stage stage = new Stage();
        stage.setScene(new Scene(vBox));
        stage.setTitle("Dayun");
        stage.setResizable(false);
        stage.showAndWait();
    }

    /**
     * ahuang事件
     * 弹出图片
     *
     * @param actionEvent 事件
     */
    @FXML
    public void openAhuang(ActionEvent actionEvent) {
        VBox vBox = new VBox();
        vBox.getChildren().addAll(new ImageView("/pics/Ahuang.jpg"));
        Stage stage = new Stage();
        stage.setScene(new Scene(vBox));
        stage.setTitle("ahuang");
        stage.setResizable(false);
        stage.showAndWait();
    }

    /**
     * potato事件
     * 弹出图片
     *
     * @param actionEvent 事件
     */
    @FXML
    public void openPotato(ActionEvent actionEvent) {
        VBox vBox = new VBox();
        vBox.getChildren().addAll(new ImageView("/pics/potato.jpg"));
        Stage stage = new Stage();
        stage.setScene(new Scene(vBox));
        stage.setTitle("potato");
        stage.setResizable(false);
        stage.showAndWait();
    }

    /**
     * Zheng事件
     * 弹出图片
     *
     * @param actionEvent 事件
     */
    @FXML
    public void openZheng(ActionEvent actionEvent) {
        VBox vBox = new VBox();
        vBox.getChildren().addAll(new ImageView("/pics/Zheng.jpg"));
        Stage stage = new Stage();
        stage.setScene(new Scene(vBox));
        stage.setTitle("Zheng");
        stage.setResizable(false);
        stage.showAndWait();
    }
}